package com.lj.back.manage.scaffold.module.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lj.back.manage.scaffold.common.DTO.PageQuery;
import com.lj.back.manage.scaffold.common.util.Assert;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.common.util.QueryWrapperUtil;
import com.lj.back.manage.scaffold.module.sys.DO.SysPermDO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysPermDTO;
import com.lj.back.manage.scaffold.module.sys.dao.SysPermDao;
import com.lj.back.manage.scaffold.module.sys.service.SysPermService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后台管理---权限service实现类
 * @author lv
 * @version 1.0.0
 */
@Service
@AllArgsConstructor
public class SysPermServiceImpl implements SysPermService {

    private final SysPermDao sysPermDao;

    @Override
    public Set<String> listPermissionsByMenu(Long menuId) {
        Set<String> perms = sysPermDao.selectPermByMenu(menuId);
        return perms;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public Long addPerm(SysPermDTO perm){
        //新增权限表数据
        SysPermDO permDO = BeanUtil.copyProperties(perm, SysPermDO.class);
        sysPermDao.insert(permDO);
        return permDO.getId();
    }

    @Override
    public boolean updatePerm(SysPermDTO perm){
        //不允许修改权限名称
        perm.setName(null);
        SysPermDO sysPermDO = BeanUtil.copyProperties(perm, SysPermDO.class);
        sysPermDao.updateById(sysPermDO);
        return true;
    }

    @Override
    public IPage<SysPermDTO> listPerms(PageQuery pageQuery){
        QueryWrapper<SysPermDO> queryWrapper = QueryWrapperUtil.getPageQueryWrapper(SysPermDO.class, pageQuery, "name");
        IPage<SysPermDO> sysPermDOPage = sysPermDao.selectPage(QueryWrapperUtil.getPage(pageQuery),queryWrapper);
        return CommonUtil.pageBeanConvert(sysPermDOPage,SysPermDTO.class);
    }

    @Override
    public LinkedHashSet<SysPermDTO> listPerms(){
        List<SysPermDO> sysPermDOS =  sysPermDao.selectList(QueryWrapperUtil.getOrderByDescQueryWrapper());
        if (CollectionUtil.isEmpty(sysPermDOS)){
            return null;
        }
        return sysPermDOS.stream()
                .map(sysPermDO -> BeanUtil.copyProperties(sysPermDO, SysPermDTO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
    }

    @Override
    public SysPermDTO getPermInfo(Long id) {
        SysPermDO sysPermDO = sysPermDao.selectById(id);
        Assert.assertNotNull(sysPermDO,"权限ID不存在");
        return BeanUtil.copyProperties(sysPermDO, SysPermDTO.class);
    }
}
